﻿using Dapper;
using InventoryIssue.API.Read.Application.Command.Allocate;
using InventoryIssue.Dtos;
using InventoryIssue.ErrorCode;
using InventoryIssue.Infrastructrue.Implement;
using MediatR;
using Newtonsoft.Json;
using System.Text;

namespace InventoryIssue.API.Read.Application.Handler.Allocate
{
    public class GetAllocateHandler : IRequestHandler<GetAllocateCommand, APIPaging<AllocateDto>>
    {
        private readonly DatabaseContext db;

        public GetAllocateHandler(DatabaseContext db)
        {
            this.db = db;
        }
        public async Task<APIPaging<AllocateDto>> Handle(GetAllocateCommand request, CancellationToken cancellationToken)
        {
            APIPaging<AllocateDto> result = new APIPaging<AllocateDto>();
            StringBuilder sql = new StringBuilder();
            StringBuilder countSql = new StringBuilder();
            sql.Append(@"
                SELECT
                    a.Note,
                    a.OrderNumber,
	                a.AllocateId,
	                a.AllocateWarehouseIn,
	                a.AllocateWarehouseOut,
	                a.State,
	                a.OrderCreator,
	                a.OrderCreatorTime,
	                a.Manager,
	                a.ManagerTime,
	                b.quantity,
                    b.Id,
	                c.ProductName,
	                c.ProductCode,
                    c.Specifications,
	                d.UnitName,
	                e.DepotName,
	                f.LocationName 
                FROM
	                allocatemodel a
	                JOIN allocateproductmodel b ON a.AllocateId = b.AllocateId
	                JOIN productmodel c ON b.ProductId = c.ProductId
	                JOIN unitmodel d ON c.UnitId = d.UnitId
	                JOIN depotmodel e ON c.DepotId = e.DepotId
	                JOIN locationmodel f ON c.LocationId = f.LocationId 
                WHERE
	                a.IsDel = 0 
	                AND b.IsDel = 0 
	                AND c.IsDel = 0
                ");
            countSql.Append(@"SELECT
                  COUNT(a.Id)
                FROM
                  allocatemodel a
                  JOIN allocateproductmodel b ON a.AllocateId = b.AllocateId
                  JOIN productmodel c ON b.ProductId = c.ProductId
                  WHERE a.IsDel = 0");
            if (!string.IsNullOrEmpty(request.AllocateId))
            {
                sql.Append(" AND a.AllocateId LIKE @AllocateId");
                countSql.Append(" AND a.AllocateId LIKE @AllocateId");
            }
            if (request.OrderCreatorTime.HasValue)
            {
                sql.Append(" AND a.OrderCreatorTime>= @OrderCreatorTime");
                countSql.Append(" AND a.OrderCreatorTime>= @OrderCreatorTime");
            }
            if (request.State.HasValue)
            {
                sql.Append(" AND a.State = @State");
                countSql.Append(" AND a.State = @State");
            }
            sql.Append(" ORDER BY a.OrderCreatorTime DESC LIMIT @PageSize OFFSET @offset");
            var parameters = new
            {
                AllocateId = $"%{request.AllocateId}%",
                OrderCreatorTime = request.OrderCreatorTime,
                State = request.State,
                PageSize = request.PageSize,
                offset = (request.PageIndex - 1) * request.PageSize
            };
            var pageDatas = await db.DapperConnection.QueryAsync<AllocateDto>(sql.ToString(), parameters);
            int totalCount = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parameters);
            int totalPage = (int)Math.Ceiling(totalCount / (double)request.PageSize);
            result.Message = "显示成功";
            result.TotalPage = totalPage;
            result.TotalCount = totalCount;
            result.PageDatas = pageDatas.ToList();
            return result;
        }
    }
}
